package cn.harry.controller.wx;

import cn.harry.common.annotation.SysLog;
import cn.harry.common.api.CommonPage;
import cn.harry.common.api.CommonResult;
import cn.harry.common.enums.BusinessType;
import cn.harry.wx.entity.WxQrCode;
import cn.harry.wx.form.WxQrCodeForm;
import cn.harry.wx.service.WxQrCodeService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.result.WxMpQrCodeTicket;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;


/**
 * 公众号带参二维码
 *
 * @author honghh
 * Date 2021-04-08 16:19:27
 * Copyright (C) www.tech-harry.cn
 */
@Api(tags = "wx-qrcode => 公众号带参二维码")
@RestController
@RequestMapping("wx/qrcode")
public class WxQrCodeController {
    @Resource
    private WxQrCodeService wxQrCodeService;
    @Autowired
    private WxMpService wxMpService;

    /**
     * 创建带参二维码ticket
     */
    @PostMapping("/createTicket")
    @PreAuthorize("@ss.hasPermi('wx:qrcode:add')")
    @ApiOperation(value = "创建带参二维码ticket", notes = "ticket可以换取二维码图片")
    public CommonResult<WxMpQrCodeTicket> createTicket(@CookieValue String appid, @RequestBody WxQrCodeForm form) throws WxErrorException {
        wxMpService.switchoverTo(appid);
        WxMpQrCodeTicket ticket = wxQrCodeService.createQrCode(appid, form);
        return CommonResult.success(ticket);
    }

    @ApiOperation("list => 分页获取公众号带参二维码列表")
    @PreAuthorize("@ss.hasPermi('wx:qrcode:list')")
    @GetMapping(value = "/list")
    public CommonResult<CommonPage<WxQrCode>> list(@CookieValue String appid,WxQrCode wxQrCode,
                                                   @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                                   @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        wxQrCode.setAppid(appid);
        IPage<WxQrCode> page = wxQrCodeService.getPage(wxQrCode, pageSize, pageNum);
        return CommonResult.success(CommonPage.restPage(page));
    }

    /**
     * 查询公众号带参二维码详细
     */
    @ApiOperation("{id} => 获取公众号带参二维码信息")
    @PreAuthorize("@ss.hasPermi('wx:qrcode:query')")
    @GetMapping(value = "/{id}")
    public CommonResult<WxQrCode> getInfo(@PathVariable Long id) {
        return CommonResult.success(wxQrCodeService.selectById(id));
    }

    @ApiOperation("create => 新建公众号带参二维码")
    @PreAuthorize("@ss.hasPermi('wx:qrcode:add')")
    @PostMapping(value = "/create")
    public CommonResult<Integer> create(@RequestBody WxQrCode wxQrCode) {
        int count = wxQrCodeService.create(wxQrCode);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    @ApiOperation("update/{id} => 修改指定公众号带参二维码")
    @PreAuthorize("@ss.hasPermi('wx:qrcode:edit')")
    @SysLog(title = "公众号带参二维码数据", businessType = BusinessType.UPDATE)
    @PutMapping(value = "/update/{id}")
    public CommonResult<Integer> update(@PathVariable Long id, @RequestBody WxQrCode wxQrCode) {
        int count = wxQrCodeService.update(id, wxQrCode);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }


    @ApiOperation("deleteByIds/{ids} => 删除指定公众号带参二维码")
    @PreAuthorize("@ss.hasPermi('wx:qrcode:remove')")
    @SysLog(title = "公众号带参二维码类型", businessType = BusinessType.DELETE)
    @DeleteMapping(value = "/deleteByIds/{ids}")
    public CommonResult<Integer> deleteByIds(@PathVariable Long[] ids) {
        int count = wxQrCodeService.deleteByIds(ids);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }
}
